﻿using System.Reflection;
using FluentNHibernate;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace NotMyself.Oxite.Data.LinqToNHibernateProvider
{
    internal class SessionFactoryProvider
    {
        private readonly ISessionFactory _sessionFactory;

        public SessionFactoryProvider()
        {
            _sessionFactory = GetSessionFactory();
        }

        public ISession OpenSession()
        {
            return _sessionFactory.OpenSession();
        }

        private ISessionFactory GetSessionFactory()
        {
            Configuration cfg = new Configuration().Configure();
            var persistence = new PersistenceConfiguration();
            persistence.Configure(cfg);
            return cfg.BuildSessionFactory();
        }

        internal static void ExportSchema()
        {
            Configuration cfg = new Configuration().Configure();
            var persistence = new PersistenceConfiguration();
            persistence.Configure(cfg);
            new SchemaExport(cfg).Create(true, true);
        }
    }
}